﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.ExternalConnections.CUiECEditSimbaPnioConnectionForm
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using DnProcessSimulateCommands.ExternalConnections.Res;
using EngineeringInternalExtension;
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Tecnomatix.Engineering.Ui;


namespace DnProcessSimulateCommands.ExternalConnections;

internal class CUiECEditSimbaPnioConnectionForm : TxForm
{
  private CApECExternalConnectionsManager m_dataMgr;
  private CApECSimbaConnectionFormParams m_params;
  private int m_suspendEnablersCounter;
  private IContainer components;
  private Label m_labelName;
  private TextBox m_textBoxName;
  private Button m_buttonCancel;
  private Button m_buttonOK;
  private Label HW_index;
  private NumericUpDown m_hwIndex;
  private Label ItemHeaderLabel;
  private TextBox m_textBoxItemHeader;

  internal CUiECEditSimbaPnioConnectionForm(CApECExternalConnectionsManager mgr)
  {
    this.InitializeComponent();
    this.m_dataMgr = mgr;
    this.RegisterForControlEvents();
  }

  internal DialogResult ShowDialog(CApECSimbaConnectionFormParams formParams)
  {
    this.InitMode(formParams);
    return ((Form) this).ShowDialog();
  }

  private void InitMode(CApECSimbaConnectionFormParams formParams)
  {
    this.m_params = formParams;
    this.SuspendUiEnablers();
    try
    {
      switch (this.m_params.DialogMode)
      {
        case CApECSimbaConnectionFormParams.eMode.Edit:
          this.InitUiByData(this.m_params.ConnectionToEdit.Data, false);
          ((Control) this).Text = CUiECExternalConnectionsResourceTable.EDIT_SIMBA_PNIO_CONNECTION_DLG_CAPTION;
          this.m_textBoxName.Focus();
          this.m_textBoxName.SelectAll();
          break;
        case CApECSimbaConnectionFormParams.eMode.Create:
          this.InitUiByData((ITxPlcExternalConnectionDataEx) new TxPlcSimbaConnectionDataEx(), false);
          ((Control) this).Text = CUiECExternalConnectionsResourceTable.ADD_SIMBA_PNIO_CONNECTION_DLG_CAPTION;
          this.m_textBoxName.Focus();
          break;
      }
    }
    finally
    {
      this.ResumeUiEnablers();
    }
  }

  private void InitUiByData(ITxPlcExternalConnectionDataEx data, bool updateEnablers)
  {
    IApECExternalConnectionDataConvertor instance = this.m_params.DataConvertor.CreateInstance(data);
    this.m_textBoxName.Text = instance.Name as string;
    this.m_hwIndex.Value = (Decimal) (int) instance.HardwareIndex;
    if (this.m_hwIndex.Value == 0M)
      this.m_textBoxItemHeader.Text = "S000";
    if (!updateEnablers)
      return;
    this.UpdateAllEnablers();
  }

  private ITxPlcExternalConnectionDataEx RetrieveDataFromUi()
  {
    IApECExternalConnectionDataConvertor instance = this.m_params.DataConvertor.CreateInstance((ITxPlcExternalConnectionDataEx) new TxPlcSimbaConnectionDataEx());
    instance.Name = (object) this.m_textBoxName.Text;
    instance.HardwareIndex = (object) (int) this.m_hwIndex.Value;
    return instance.Data;
  }

  private void RegisterForControlEvents()
  {
    this.m_buttonOK.Click += new EventHandler(this.m_buttonOK_Click);
    this.m_textBoxName.TextChanged += new EventHandler(this.m_textBoxName_TextChanged);
    this.m_hwIndex.ValueChanged += new EventHandler(this.m_hwIndex_ValueChanged);
  }

  private void UnRegisterForControlEvents()
  {
    this.m_buttonOK.Click -= new EventHandler(this.m_buttonOK_Click);
    this.m_textBoxName.TextChanged -= new EventHandler(this.m_textBoxName_TextChanged);
    this.m_hwIndex.ValueChanged -= new EventHandler(this.m_hwIndex_ValueChanged);
  }

  private bool EnablersSuspended() => this.m_suspendEnablersCounter > 0;

  private void SuspendUiEnablers() => ++this.m_suspendEnablersCounter;

  private void ResumeUiEnablers()
  {
    if (this.EnablersSuspended())
      --this.m_suspendEnablersCounter;
    if (this.EnablersSuspended())
      return;
    this.UpdateAllEnablers();
  }

  private void UpdateAllEnablers()
  {
    if (this.EnablersSuspended())
      return;
    this.UpdateEnabler_ButtonOK();
  }

  private void UpdateEnabler_ButtonOK()
  {
    if (this.EnablersSuspended())
      return;
    this.m_buttonOK.Enabled = this.m_textBoxName.Text.Trim().Length > 0;
  }

  protected virtual void OnLoad(EventArgs e)
  {
    ((Control) this).MinimumSize = new Size(((Control) this).Width, ((Control) this).Height);
    ((Control) this).MaximumSize = new Size(int.MaxValue, ((Control) this).Height);
    base.OnLoad(e);
  }

  protected virtual void OnClosing(CancelEventArgs e)
  {
    if (((Form) this).DialogResult == DialogResult.OK)
    {
      ITxPlcExternalConnectionDataEx connectionDataEx = this.RetrieveDataFromUi();
      switch (this.m_params.DialogMode)
      {
        case CApECSimbaConnectionFormParams.eMode.Edit:
          CApECExternalConnectionsManager.eSetConnectionDataFailiureReason failiureReason1;
          if (!this.m_dataMgr.CanSetConnectionData(this.m_params.ConnectionToEdit, connectionDataEx, out failiureReason1))
          {
            e.Cancel = true;
            if (failiureReason1 == CApECExternalConnectionsManager.eSetConnectionDataFailiureReason.DuplicatedName)
            {
              int num = (int) MessageBox.Show(CUiECExternalConnectionsResourceTable.ERR_MSG_DUPLICATED_CONNECTION_NAME, ((Control) this).Text, MessageBoxButtons.OK, MessageBoxIcon.Hand);
              break;
            }
            break;
          }
          break;
        case CApECSimbaConnectionFormParams.eMode.Create:
          CApECExternalConnectionsManager.eCreateConnectionFailiureReason failiureReason2;
          if (!this.m_dataMgr.CanCreateConnectionWithData(connectionDataEx, out failiureReason2))
          {
            e.Cancel = true;
            if (failiureReason2 == CApECExternalConnectionsManager.eCreateConnectionFailiureReason.DuplicatedName)
            {
              int num = (int) MessageBox.Show(CUiECExternalConnectionsResourceTable.ERR_MSG_DUPLICATED_CONNECTION_NAME, ((Control) this).Text, MessageBoxButtons.OK, MessageBoxIcon.Hand);
              break;
            }
            break;
          }
          break;
      }
      if (!e.Cancel)
        this.m_params.DataToApply = connectionDataEx;
      else
        ((Form) this).DialogResult = DialogResult.Cancel;
    }
    base.OnClosing(e);
  }

  private void m_buttonOK_Click(object sender, EventArgs e)
  {
    ((Form) this).DialogResult = DialogResult.OK;
    if (((Form) this).Modal)
      return;
    ((Form) this).Close();
  }

  private void m_buttonCancel_Click(object sender, EventArgs e)
  {
    ((Form) this).DialogResult = DialogResult.Cancel;
    if (((Form) this).Modal)
      return;
    ((Form) this).Close();
  }

  private void m_textBoxName_TextChanged(object sender, EventArgs e)
  {
    this.UpdateEnabler_ButtonOK();
  }

  private void m_hwIndex_ValueChanged(object sender, EventArgs e)
  {
    int num = (int) (sender as NumericUpDown).Value;
    string str = "S0";
    if (num - 10 < 0)
      str += "0";
    this.m_textBoxItemHeader.Text = str + num.ToString();
  }

  private void CUiECEditSimbaPnioConnectionForm_Load(object sender, EventArgs e)
  {
  }

  protected virtual void Dispose(bool disposing)
  {
    if (disposing && this.components != null)
      this.components.Dispose();
    base.Dispose(disposing);
  }

  private void InitializeComponent()
  {
    ComponentResourceManager componentResourceManager = new ComponentResourceManager(typeof (CUiECEditSimbaPnioConnectionForm));
    this.m_labelName = new Label();
    this.m_textBoxName = new TextBox();
    this.m_buttonCancel = new Button();
    this.m_buttonOK = new Button();
    this.HW_index = new Label();
    this.m_hwIndex = new NumericUpDown();
    this.ItemHeaderLabel = new Label();
    this.m_textBoxItemHeader = new TextBox();
    this.m_hwIndex.BeginInit();
    ((Control) this).SuspendLayout();
    componentResourceManager.ApplyResources((object) this.m_labelName, "m_labelName");
    this.m_labelName.Name = "m_labelName";
    componentResourceManager.ApplyResources((object) this.m_textBoxName, "m_textBoxName");
    this.m_textBoxName.Name = "m_textBoxName";
    this.m_buttonCancel.DialogResult = DialogResult.Cancel;
    componentResourceManager.ApplyResources((object) this.m_buttonCancel, "m_buttonCancel");
    this.m_buttonCancel.Name = "m_buttonCancel";
    this.m_buttonCancel.UseVisualStyleBackColor = true;
    componentResourceManager.ApplyResources((object) this.m_buttonOK, "m_buttonOK");
    this.m_buttonOK.Name = "m_buttonOK";
    this.m_buttonOK.UseVisualStyleBackColor = true;
    componentResourceManager.ApplyResources((object) this.HW_index, "HW_index");
    this.HW_index.Name = "HW_index";
    componentResourceManager.ApplyResources((object) this.m_hwIndex, "m_hwIndex");
    this.m_hwIndex.Maximum = new Decimal(new int[4]
    {
      31 /*0x1F*/,
      0,
      0,
      0
    });
    this.m_hwIndex.Name = "m_hwIndex";
    componentResourceManager.ApplyResources((object) this.ItemHeaderLabel, "ItemHeaderLabel");
    this.ItemHeaderLabel.Name = "ItemHeaderLabel";
    componentResourceManager.ApplyResources((object) this.m_textBoxItemHeader, "m_textBoxItemHeader");
    this.m_textBoxItemHeader.Name = "m_textBoxItemHeader";
    this.m_textBoxItemHeader.ReadOnly = true;
    ((Form) this).AcceptButton = (IButtonControl) this.m_buttonOK;
    componentResourceManager.ApplyResources((object) this, "$this");
    ((ContainerControl) this).AutoScaleMode = AutoScaleMode.Font;
    ((Form) this).CancelButton = (IButtonControl) this.m_buttonCancel;
    ((Control) this).Controls.Add((Control) this.m_textBoxItemHeader);
    ((Control) this).Controls.Add((Control) this.ItemHeaderLabel);
    ((Control) this).Controls.Add((Control) this.m_hwIndex);
    ((Control) this).Controls.Add((Control) this.HW_index);
    ((Control) this).Controls.Add((Control) this.m_buttonCancel);
    ((Control) this).Controls.Add((Control) this.m_buttonOK);
    ((Control) this).Controls.Add((Control) this.m_textBoxName);
    ((Control) this).Controls.Add((Control) this.m_labelName);
    ((Form) this).MaximizeBox = false;
    ((Form) this).MinimizeBox = false;
    ((Control) this).Name = nameof (CUiECEditSimbaPnioConnectionForm);
    ((Form) this).ShowIcon = false;
    ((Form) this).SizeGripStyle = SizeGripStyle.Hide;
    ((Form) this).Load += new EventHandler(this.CUiECEditSimbaPnioConnectionForm_Load);
    this.m_hwIndex.EndInit();
    ((Control) this).ResumeLayout(false);
    ((Control) this).PerformLayout();
  }
}
